home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / upc12bs2.zip / UTIL / novrstrk.c < prev    next >
C/C++ Source or Header  |  1993-04-10  |  6KB  |  178 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    Program:    noverstrk.c     14 August 1990                      */
  3. /*    Author:     Andrew H. Derbyshire                                */
  4. /*                108 Decatur St, Apt 9                               */
  5. /*                Arlington, MA 02174                                 */
  6. /*    Function:   Drop overstruck characters from a file              */
  7. /*    Arguments:  input file, output file.                            */
  8. /*--------------------------------------------------------------------*/
  9.  
  10. /*--------------------------------------------------------------------*/
  11. /*                          RCS information                           */
  12. /*--------------------------------------------------------------------*/
  13.  
  14. /*
  15.    $Log: NOVRSTRK.C $
  16.  * Revision 1.2  1993/04/11  00:33:54  ahd
  17.  * Global edits for year, TEXT, etc.
  18.  *
  19.  * Revision 1.1  1992/11/15  04:29:22  ahd
  20.  * Initial revision
  21.  *
  22.  * Revision 1.1  1992/04/27  00:27:34  ahd
  23.  * Initial revision
  24.  *
  25.  */
  26.  
  27. static char rcsid[] =
  28.          "$Id: NOVRSTRK.C 1.2 1993/04/11 00:33:54 ahd Exp $";
  29.  
  30. /*--------------------------------------------------------------------*/
  31. /*    Revised 10 March 1991 to handle overstriking via carriage       */
  32. /*    returns, which is how Word for Windows does it                  */
  33. /*--------------------------------------------------------------------*/
  34.  
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <stdio.h>
  38.  
  39. #include "lib.h"
  40. #include "timestmp.h"
  41.  
  42. /*--------------------------------------------------------------------*/
  43. /*    m a i n                                                         */
  44. /*                                                                    */
  45. /*    Main program                                                    */
  46. /*--------------------------------------------------------------------*/
  47.  
  48.  void main( int argc, char *argv[] )
  49.  {
  50.    char buf[BUFSIZ];       /* Our I/O buffer                         */
  51.    size_t column = 0;
  52.    size_t linesize = 0;
  53.    int argx = 1;           /* Current argument being processed       */
  54.    FILE *input;
  55.    FILE *output;
  56.    int done = 0;
  57.    int ignore = 0;
  58.  
  59. /*--------------------------------------------------------------------*/
  60. /*                        Announce our version                        */
  61. /*--------------------------------------------------------------------*/
  62.  
  63.    banner( argv );
  64.  
  65. /*--------------------------------------------------------------------*/
  66. /*                            Handle help                             */
  67. /*--------------------------------------------------------------------*/
  68.  
  69.    if (( argc > 1 ) && equal(argv[1],"-?"))
  70.    {
  71.       printf("Usage:\tnovrstrk\t infile outfile\n");
  72.       exit(1);
  73.    }
  74. /*--------------------------------------------------------------------*/
  75. /*                  Get the input file name, if any                   */
  76. /*--------------------------------------------------------------------*/
  77.  
  78.     if (argx == argc)
  79.       input = stdin;
  80.     else {
  81.       input = fopen(argv[argx++],"rb");
  82.       if (input == NULL)
  83.       {
  84.          perror(argv[--argx]);
  85.          exit (100);
  86.       }
  87.     }
  88.  
  89. /*--------------------------------------------------------------------*/
  90. /*                    Get output file name, if any                    */
  91. /*--------------------------------------------------------------------*/
  92.  
  93.     if (argx == argc )
  94.       output = stdout;
  95.     else {
  96.       output = fopen(argv[argx++],"w");
  97.       if (output == NULL)
  98.       {
  99.          perror(argv[--argx]);
  100.          exit( 200 );
  101.       } /* if */
  102.     } /* else */
  103.  
  104. /*--------------------------------------------------------------------*/
  105. /*       Main loop to drop our overstrikes                            */
  106. /*--------------------------------------------------------------------*/
  107.  
  108.     while (!done)
  109.     {
  110.       char c = fgetc( input );
  111.       if ( feof(input) || ferror(input) )
  112.          done = 1;
  113.       else switch (c)
  114.       {
  115.          case '\b':           /* Simple overstrike?                  */
  116.             ignore = 1;       /* Yes --> Ignore it                   */
  117.             break;
  118.  
  119.          case '\r':           /* Carriage return?                    */
  120.             if ( column > linesize )
  121.                linesize = column;
  122.             column = 0;       /* Yes --> Start line over again       */
  123.             break;
  124.  
  125.          case '\f':           /* Form feed?                          */
  126.          case '\n':           /* New line?                           */
  127.             if ( column > linesize )
  128.                linesize = column;
  129.             buf[linesize++] = c;  /* Yes --> Add the end of line     */
  130.             buf[linesize] = '\0'; /*   ... terminate the buffer      */
  131.             fputs(buf, output);   /*   ... write it out              */
  132.             done = ferror(output);/*   ... check the result          */
  133.             linesize = column = 0;/*   ... and begin a new buffer    */
  134.             break;
  135.  
  136.          default:
  137.             if ( ignore )
  138.                ignore = 0;
  139.             else if (( linesize > column ) && (c == ' '))
  140.                column += 1;
  141.             else
  142.                buf[ column++ ] = c; /* Add the character to the buf */
  143.        break;
  144.        } /* switch */
  145.     } /* while */
  146.  
  147. /*--------------------------------------------------------------------*/
  148. /*                   Check for errors on the files                    */
  149. /*--------------------------------------------------------------------*/
  150.  
  151.    if (ferror(input))
  152.    {
  153.       perror(argv[argc > 1 ? 1 : 0]);
  154.       clearerr(input);
  155.    }
  156.  
  157.    if ((column > 0) && !ferror(output))
  158.    {
  159.       fputs(buf, output );
  160.       buf[linesize] = '\0';
  161.    }
  162.  
  163.    if (ferror(output))
  164.    {
  165.       perror(argv[argc > 2 ? 2 : 0]);
  166.       clearerr(output);
  167.    }
  168.  
  169. /*--------------------------------------------------------------------*/
  170. /*                       Close up shop and exit                       */
  171. /*--------------------------------------------------------------------*/
  172.  
  173.    fclose(input);
  174.    fclose(output);
  175.  
  176.    exit (0);
  177.  } /* main */
  178.